//
//  CarouselView.m
//  MyFramework
//
//  Created by 余洋 on 2018/9/13.
//  Copyright © 2018年 余洋. All rights reserved.
//

#import "CarouselView.h"

#define CAROUSEL_SCREEN_WIDTH ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)]?[UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale:[UIScreen mainScreen].bounds.size.width)

@interface CarouselView()<UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
//定时器
@property (nonatomic, strong) NSTimer *timer;
//左中右三个imageView
@property (nonatomic, strong) UIImageView *imageViewLeft;
@property (nonatomic, strong) UIImageView *imageViewMiddle;
@property (nonatomic, strong) UIImageView *imageViewRight;
//分页
@property (nonatomic, assign) NSInteger currentImageIndex;

@end

@implementation CarouselView

@synthesize timeInterval = _timeInterval;
@synthesize picUrlArray = _picUrlArray;
@synthesize contentMode = _contentMode;

- (void)awakeFromNib {
    [super awakeFromNib];
    _timeInterval = 3.f;
    [self configurationScrollView];
}

#pragma mark - UI

/**
 配置scrollView
 */
- (void)configurationScrollView {
    [self.scrollView setContentOffset:CGPointMake(CAROUSEL_SCREEN_WIDTH, 0) animated:NO];
    //设置分页
    self.scrollView.pagingEnabled = YES;
    //隐藏滚动条
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    //不需要回弹 如果带回弹的话 快速滑动会出现图片还未加载完成的情况
    self.scrollView.bounces = NO;
    self.scrollView.delegate = self;
}

/**
 配置UI
 */
- (void)createUI {
    _currentImageIndex = 0;
    //设置pageControl
    self.pageControl.numberOfPages = _picUrlArray.count;
    //配置ImageView
    CGFloat imageHeight = self.superview.frame.size.height;
    CGFloat imageWidth = self.superview.frame.size.width;
    //配置左、中、右3个imageView
    self.imageViewLeft.frame = CGRectMake(0, 0, imageWidth, imageHeight);
    [self.scrollView addSubview:self.imageViewLeft];
    self.imageViewMiddle.frame = CGRectMake(imageWidth, 0, imageWidth, imageHeight);
    [self.scrollView addSubview:self.imageViewMiddle];
    self.imageViewRight.frame = CGRectMake(2 * imageWidth, 0, imageWidth, imageHeight);
    [self.scrollView addSubview:self.imageViewRight];
    [self reloadImages];
    //只有图片大于1张的时候才执行定时任务
    if (_picUrlArray.count > 1) {
        self.scrollView.contentSize = CGSizeMake(3 * CAROUSEL_SCREEN_WIDTH, 0);
        [self startTimer];
    } else {
        self.scrollView.contentSize = CGSizeMake(CAROUSEL_SCREEN_WIDTH, 0);
    }
}

- (void)setImageContentMode {
    [self.imageViewLeft setContentMode:_contentMode];
    [self.imageViewMiddle setContentMode:_contentMode];
    [self.imageViewRight setContentMode:_contentMode];
}

#pragma mark — 定时器
/**
 开启定时器
 */
- (void)startTimer {
     self.timer = [NSTimer scheduledTimerWithTimeInterval:_timeInterval target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
}

/**
 关闭定时器
 */
- (void)stopTimer {
    [self.timer invalidate];
}

#pragma mark — action
- (void)nextImage {
    //滚动scrollView
    CGFloat x = self.scrollView.frame.size.width;
    [self.scrollView setContentOffset:CGPointMake(2 * x, 0) animated:YES];
}

- (void)reloadImages {
    CGPoint offset = [self.scrollView contentOffset];
    if (offset.x == 2 * CAROUSEL_SCREEN_WIDTH) {//左滑
        _currentImageIndex = (_currentImageIndex + 1) % _picUrlArray.count;
    } else if (offset.x == 0) {//右滑
        _currentImageIndex = (_currentImageIndex-1 < 0? _picUrlArray.count-1 : _currentImageIndex-1) % _picUrlArray.count;
    }
    self.imageViewMiddle.image = _picUrlArray[_currentImageIndex];
    NSInteger leftImageIndex = (_currentImageIndex-1 < 0? _picUrlArray.count-1 : _currentImageIndex-1) % _picUrlArray.count;
    NSInteger rightImageIndex = (_currentImageIndex + 1) % _picUrlArray.count;
    self.imageViewLeft.image = _picUrlArray[leftImageIndex];
    self.imageViewRight.image = _picUrlArray[rightImageIndex];
}

- (void)clickImageView {
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickImage:)]) {
        //调用代理
        [self.delegate clickImage:_currentImageIndex];
    }
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self reloadImages];
    [scrollView setContentOffset:CGPointMake(CAROUSEL_SCREEN_WIDTH, 0) animated:NO];
    self.pageControl.currentPage = _currentImageIndex;
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    [self reloadImages];
    [scrollView setContentOffset:CGPointMake(CAROUSEL_SCREEN_WIDTH, 0) animated:NO];
    self.pageControl.currentPage = _currentImageIndex;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self stopTimer];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    [self startTimer];
}

#pragma mark - set
- (void)setPicUrlArray:(NSMutableArray *)picUrlArray {
    if (picUrlArray) {
        _picUrlArray = picUrlArray;
        [self createUI];
    }
}

- (void)setTimeInterval:(NSTimeInterval)timeInterval {
    if (timeInterval) {
        _timeInterval = timeInterval;
        [self stopTimer];
        [self startTimer];
    }
}

- (void)setContentMode:(UIViewContentMode)contentMode {
    if (contentMode) {
        _contentMode = contentMode;
        [self setImageContentMode];
    }
}

#pragma mark 懒加载
- (UIImageView *)imageViewLeft {
    if (_imageViewLeft == nil) {
        _imageViewLeft = [[UIImageView alloc] init];
        _imageViewLeft.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickImageView)];
        [_imageViewLeft addGestureRecognizer:tapGesture];
    }
    return _imageViewLeft;
}

- (UIImageView *)imageViewMiddle {
    if (_imageViewMiddle == nil) {
        _imageViewMiddle = [[UIImageView alloc] init];
        _imageViewMiddle.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickImageView)];
        [_imageViewMiddle addGestureRecognizer:tapGesture];
    }
    return _imageViewMiddle;
}

- (UIImageView *)imageViewRight {
    if (_imageViewRight == nil) {
        _imageViewRight = [[UIImageView alloc] init];
        _imageViewRight.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickImageView)];
        [_imageViewRight addGestureRecognizer:tapGesture];
    }
    return _imageViewRight;
}

@end
